/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.ks.client.views;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.gwtext.client.widgets.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.macdadi.core.client.auth.AuthorizationException;
import org.macdadi.core.client.domain.*;
import org.macdadi.ks.client.View;
import org.macdadi.ks.client.ViewContainer;
import org.macdadi.core.client.ui.EditableTree;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.core.ExtElement;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.AccordionLayout;  
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.tree.*;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.layout.BorderLayoutData;   

/**
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class Values extends View
{
	protected ProjectInfo project;

	protected VerticalPanel graphPanel = new VerticalPanel();
	protected VerticalPanel panel1 = new VerticalPanel();
	protected VerticalPanel panel2 = new VerticalPanel();
	protected VerticalPanel mainPanel = new VerticalPanel();
	protected Panel accordionPanel;
	protected Panel westPanel;

	TreeNode[] checkedGoalNodes;
	TreeNode[] checkedOptionNodes;
	TreeNode[] checkedPeopleNodes;
	User[] users;
	Button buttonCreateImpact;
	Button ReSelectImpacts;
	protected ArrayList expandedItems = new ArrayList();
	int user_index = 0;
	private static String instructionsString;
	private static String bugsString;
	Button instructionsButton;

	protected static final String PREFIX_URL = "http://chart.apis.google.com/chart?";

	public static ViewInfo init()
	{
		String info = "<p>Visualize the value of each option</p>";

		return new ViewInfo("Values", info)
		{
			public View createInstance(ViewContainer container)
			{
				return new Values(container);
			}

			public String getColor()
			{
				return "#C5E3BF";
			}
		};
	}

	public Values(ViewContainer container)
	{
		this.container = container;
		project = container.getCurrentProject();

		mainPanel.setStyleName("macdadi-Values");
		mainPanel.setWidth("98%");
		mainPanel.setHeight("98%");

		Panel space = new Panel();
		space.setHeight(20);
		space.setBorder(false);

		instructionsString = "<p> On this page, the user is given the option of customizing the Values bar graph. To do so: </p>" +
		"<p> ~~~ </p> " +
		"<p><b>   1) Choose which goals will affect the graph </b></p>" +
		"<p><b>   2) Choose which option-impacts will affect the graph</b></p>" +
		"<p><b>   3) Choose which project members' goals and options will affect the graph </b></p>" +
		"<p> ~~~ </p> " +
		"<p> Remarks - Here, MACDADI will use a weighted sum (between the impacts and preferences of each selected project member) to create a Values bar chart. Labels and legends are provided as well. To reset the graph simply click the Reset button. At that time the graph will disappear, the accordion panel will unmask and the user can then recustomize their graph.</p>";
		bugsString = "<p> None ATM </p>" +
		"<p> ~~~ </p>" +
		"<p> Discover any bugs? Please contact me at: <i>jwei512@stanford.edu</i> </p>";

		HTML instructionsHTML = new HTML(instructionsString);

		TabPanel tabPanel = new TabPanel();  
		tabPanel.setActiveTab(0);

		Panel tab1 = new Panel();  
		tab1.setTitle("Instructions");  
		tab1.add(instructionsHTML);
		tab1.setAutoScroll(true);  

		Panel tab2 = new Panel();  
		tab2.setTitle("Current Bugs/Fixes");  
		tab2.setHtml(bugsString);  
		tab2.setAutoScroll(true);

		tabPanel.add(tab1);
		tabPanel.add(tab2);

		BorderLayoutData centerData = new BorderLayoutData(RegionPosition.CENTER);  
		centerData.setMargins(3, 0, 3, 3);

		final Window window = new Window();
		window.setTitle("Instructions for Values Tab");  
		window.setClosable(true);  
		window.setWidth(600);  
		window.setHeight(350);  
		window.setPlain(true); 
		window.setLayout(new BorderLayout());  
		window.add(tabPanel, centerData);
		window.setCloseAction(Window.HIDE);

		instructionsButton = new Button("Instructions");  
		instructionsButton.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				window.show();
			}  
		});

		mainPanel.add(instructionsButton);
		mainPanel.add(space);

		initWidget(mainPanel);
	}

	public void onShow()
	{
		accordionPanel = createAccordionPanel();

		Panel horizontalPanel = new Panel();
		horizontalPanel.setLayout(new HorizontalLayout(20));

		Panel verticalPanel = new Panel();
		verticalPanel.setLayout(new VerticalLayout(15));  
		
		Panel space = new Panel();
		space.setHeight(50);
		space.setBorder(false);

		westPanel = new Panel();
		westPanel.add(accordionPanel);
		westPanel.setTitle("Select desired goals, options, and stakeholders.");

		//TODO: MAKE IT COLLAPSE LEFT?
		BorderLayoutData westData = new BorderLayoutData(RegionPosition.WEST);  
		westData.setSplit(true);

		horizontalPanel.add(westPanel, westData);
		graphPanel.add(panel1);
		graphPanel.add(space);
		graphPanel.add(panel2);
		horizontalPanel.add(graphPanel);
		verticalPanel.add(horizontalPanel);
		verticalPanel.addButton(ReSelectImpacts);

		mainPanel.add(verticalPanel);
	}

	public void onHide()
	{
		panel1.clear();
		panel2.clear();
		graphPanel.clear();
		accordionPanel.clear();
		accordionPanel.destroy();
		mainPanel.clear();
	}

	private Panel createAccordionPanel() {
		Panel accordionPanel = new Panel();
		accordionPanel.setLayout(new AccordionLayout(true));
		accordionPanel.setId("accordion-values");
		accordionPanel.setHeight(500);
		accordionPanel.setWidth(325);
		accordionPanel.setAnimCollapse(true);

		final TreePanel treeGoalsValues = new GoalsTree();
		treeGoalsValues.setId("tree-goals-values");
		treeGoalsValues.setTitle("Goals");
		treeGoalsValues.setWidth(325);  
		treeGoalsValues.setHeight(500);
		treeGoalsValues.setContainerScroll(true);
		treeGoalsValues.setAutoScroll(true);
		treeGoalsValues.setRootVisible(true);

		final TreePanel treeOptionsValues = new OptionsTree();
		treeOptionsValues.setId("tree-options-values");
		treeOptionsValues.setTitle("Options");
		treeOptionsValues.setWidth(325);  
		treeOptionsValues.setHeight(500);
		treeOptionsValues.setContainerScroll(true);
		treeOptionsValues.setAutoScroll(true);
		treeOptionsValues.setRootVisible(true);

		final TreePanel treePeopleValues = new PeoplesTree();
		treePeopleValues.setId("tree-peoples-values");
		treePeopleValues.setTitle("Stakeholders");
		treePeopleValues.setWidth(325);
		treePeopleValues.setHeight(500);
		treePeopleValues.setContainerScroll(true);
		treePeopleValues.setAutoScroll(true);
		treePeopleValues.setRootVisible(true);

		buttonCreateImpact = new Button("Create Graph");
		buttonCreateImpact.setEnableToggle(true);		

		ReSelectImpacts = new Button("Reset");
		ReSelectImpacts.setEnableToggle(true);
		ReSelectImpacts.disable();	

		treePeopleValues.addButton(buttonCreateImpact);

		buttonCreateImpact.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				checkedOptionNodes = treeOptionsValues.getChecked();
				checkedGoalNodes = treeGoalsValues.getChecked();
				checkedPeopleNodes = treePeopleValues.getChecked();
				final ExtElement elm1 = Ext.get("accordion-values");
				elm1.mask("GENERATING GRAPH");
				button.toggle(true);
				ReSelectImpacts.toggle(false);
				ReSelectImpacts.enable();
				populateChart();
			}
		});

		ReSelectImpacts.addListener(new ButtonListenerAdapter() {  
			public void onClick(Button button, EventObject e) {
				final ExtElement elm1 = Ext.get("accordion-values");
				elm1.unmask();
				checkedOptionNodes = null;
				checkedGoalNodes = null;
				checkedPeopleNodes = null;
				button.toggle(true);
				buttonCreateImpact.toggle(false);
				button.disable();
				graphPanel.clear();
			}
		});

		accordionPanel.add(treeGoalsValues);
		treeGoalsValues.collapse();
		accordionPanel.add(treeOptionsValues);
		treeOptionsValues.collapse();
		accordionPanel.add(treePeopleValues);

		return accordionPanel;
	}

	class GoalsTree extends TreePanel {  

		public GoalsTree() {  

			final TreeNode root = new TreeNode("Goals");
			root.setExpanded(true);

			DataService.App.getInstance().getGoals(project.getId(), container.getCurrentAuthToken(), new AsyncCallback() {
				public void onFailure(Throwable caught) {
					GWT.log("Failed to populate the goals tree", caught);
					if (caught instanceof AuthorizationException) {
						root.setText("Please login to view goals for " + project.getName());
					}
				}

				public void onSuccess(Object result) {
					root.setText("Goals");

					final Goal[] roots = (Goal[]) result;
					for (int i = 0; i < roots.length; i++) {
						addChildGoal(root,roots[i],true,roots[i].getNumChildren());
					}

					/* Expand the first and second levels */
					root.setExpanded(true);
					root.expandChildNodes(false);
				}
			});

			setTitle("Goals for" + project.getName());  
			setWidth(200);  
			setHeight(400);  
			setRootNode(root);
		}  
	}

	class OptionsTree extends TreePanel {  

		public OptionsTree() {  

			final TreeNode root = new TreeNode("Options");
			root.setExpanded(true);

			DataService.App.getInstance().getOptions(project.getId(), container.getCurrentAuthToken(), new AsyncCallback() {
				public void onFailure(Throwable caught) {
					GWT.log("Failed to populate the options tree", caught);
					if (caught instanceof AuthorizationException) {
						root.setText("Please login to view options for " + project.getName());
					}
				}

				public void onSuccess(Object result) {
					root.setText("Options");

					Option[] roots = (Option[]) result;
					for (int i = 0; i < roots.length; i++) {
						addChildOption(root,roots[i],true, roots[i].getNumChildren());
					}

					/* Expand the first and second levels */
					root.setExpanded(true);
					root.expandChildNodes(false);
				}
			});

			setTitle("Options for" + project.getName());  
			setWidth(200);  
			setHeight(400);  
			setRootNode(root);   
		}  
	}

	class PeoplesTree extends TreePanel {  

		public PeoplesTree() {  

			final TreeNode root = new TreeNode("Stakeholders");
			root.setExpanded(true);

			DataService.App.getInstance().getStakeholderGroups(project.getId(), container.getCurrentAuthToken(), new AsyncCallback() {
				public void onFailure(Throwable caught) {
					GWT.log("Failed to populate the stakeholders tree", caught);
					if (caught instanceof AuthorizationException) {
						root.setText("Please login to view stakeholders for " + project.getName());
					}
				}

				public void onSuccess(Object result) {
					root.setText("Stakeholders");

					StakeholderGroup[] roots = (StakeholderGroup[]) result;
					for (int i = 0; i < roots.length; i++) {
						addChildStakeholders(root, roots[i]);
					}

					/* Expand the first and second levels */
					root.setExpanded(true);
					root.expandChildNodes(false);

				}
			});

			setTitle("Stakeholders for" + project.getName());  
			setWidth(200);  
			setHeight(400);  
			setRootNode(root);
		}  
	}

	protected void addChildOption(TreeNode parent, Option option, boolean recurse, int leaf) {
		TreeNode node = new TreeNode(option.getName());
		node.setUserObject(option);
		if(leaf == 0) node.setChecked(true);
		node.setId(Integer.toString(option.getId()).trim());
		parent.appendChild(node);

		if (recurse) {
			for (int i = 0; i < option.getNumChildren(); i++) {
				addChildOption(node, option.getChild(i), true, option.getChild(i).getNumChildren());
			}
		}

		/* If this node, as identified by its option's id, was "expanded" in a
		 * previous incarnation of this tree, then expand it here again.
		 */
		if (expandedItems.contains(Integer.toString(option.getId())))
			node.setExpanded(true);
	}

	protected void addChildGoal(TreeNode parent, Goal goal, boolean recurse, int leaf) {
		TreeNode node = new TreeNode(goal.getName());
		node.setUserObject(goal);
		if(leaf == 0) node.setChecked(true);
		parent.appendChild(node);

		if (recurse) {
			for (int i = 0; i < goal.getNumChildren(); i++) {
				addChildGoal(node, goal.getChild(i), true, goal.getChild(i).getNumChildren());
			}
		}

		/* If this node, as identified by its option's id, was "expanded" in a
		 * previous incarnation of this tree, then expand it here again.
		 */
		if (expandedItems.contains(Integer.toString(goal.getId())))
			node.setExpanded(true);
	}

	protected void addChildStakeholders(TreeNode parent, StakeholderGroup child) {
		TreeNode node = new TreeNode(child.getName());
		node.setUserObject(child);
		parent.appendChild(node);

		for (int i = 0; i < child.getNumMembers(); i++) {
			addChildStakeholders(node, child.getMember(i));
		}

		/* If this node, as identified by its option's id, was "expanded" in a
		 * previous incarnation of this tree, then expand it here again.
		 */
		if (expandedItems.contains(Integer.toString(child.getId())))
			node.setExpanded(true);
	}

	protected void addChildStakeholders(TreeNode parent, User child) {
		TreeNode node = new TreeNode(child.getName());
		node.setUserObject(child);
		node.setChecked(true);
		parent.appendChild(node);
	}

	protected void populateChart()
	{
		project = container.getCurrentProject();

		DataService.App.getInstance().getImpacts(project.getId(), container.getCurrentAuthToken(), new AsyncCallback()
		{
			public void onFailure(Throwable caught)
			{
				GWT.log("Failed to populate the values table", caught);
				if (caught instanceof AuthorizationException) {
					// TODO: do not use Window.alert for error feedback
					System.out.println("Please login to view values for " + project.getName());
				}
			}

			public void onSuccess(Object result)
			{
				ImpactMatrix matrix = (ImpactMatrix) result;
				setupChart(matrix);
			}
		});
	}

	protected void setupChart(final ImpactMatrix matrix) {
		project = container.getCurrentProject();

		final Option[] optionsArray = getOptions(checkedOptionNodes, matrix);
		final WeightedGoal[] goalsArray = getGoals(checkedGoalNodes, matrix);


		DataService.App.getInstance().getUsers(project.getId(), container.getCurrentAuthToken(), new AsyncCallback() {

			final double[] summedPreferences = new double[goalsArray.length];

			public void onFailure(Throwable caught) {
				GWT.log("Failed to obtain project members", caught);
			}

			public void onSuccess(Object result) {
				User[] allMembers = (User[]) result;
				users = new User[0];
				for(int i = 0; i < checkedPeopleNodes.length; i++)  {
					for(int j = 0; j < allMembers.length; j++) {
						if(checkedPeopleNodes[i].getText().trim().equals(allMembers[j].getName().trim())) {
							User[] userTemp = new User[users.length+1];
							for(int k = 0; k < users.length; k++) {
								userTemp[k] = users[k];
							}
							userTemp[users.length] = allMembers[j];
							users = userTemp;
						}
					}
				}

				for(int i = 0; i < users.length; i++) {
					DataService.App.getInstance().getPreferencesForUser(project.getId(), users[i].getId(), container.getCurrentAuthToken(), new AsyncCallback(){
						public void onFailure(Throwable caught) {
							GWT.log("Failed to get preferences for user", caught);
						}

						public void onSuccess(Object result) {
							int index = 0;
							Preference[] prefs = (Preference[]) result;
							for(int j = 0; j < goalsArray.length; j++) {
								for(int k = 0; k < prefs.length; k++) {
									if(goalsArray[j].getName().trim().equals(prefs[k].getGoal().getName().trim())) {
										summedPreferences[index] += (double) prefs[k].getValue()/users.length;
										index++;
										break;
									}
								}
							}
							user_index = drawChart(summedPreferences, optionsArray, goalsArray, user_index, users.length);
						}
					});
				}
			}
		});
	}

	protected int drawChart(final double[] summedPreferences, final Option[] optionsArray, final WeightedGoal[] goalsArray, int user_index, int length) {
		user_index++;
		if(user_index == length) {
			DataService.App.getInstance().getImpacts(project.getId(), container.getCurrentAuthToken(), new AsyncCallback()
			{  	
				public void onFailure(Throwable caught)
				{
					GWT.log("Failed to obtain aggregated values", caught);
				}

				public void onSuccess(Object result)
				{
					double[][] values = new double[goalsArray.length][optionsArray.length];
					ImpactMatrix matrix = (ImpactMatrix) result;

					String[] xLabels = new String[optionsArray.length];
					for (int i = 0; i < optionsArray.length; i++) {
						xLabels[i] = optionsArray[i].getName();
						//optionsArray.length-1-i
					}

					String value_names = "";

					for(int i= optionsArray.length; i > 0; i--) {
						if(i == 1) {
							value_names = value_names + xLabels[i-1];
						} else
							value_names = value_names + xLabels[i-1] + "|";
					}

					String[] legendLabels = new String[goalsArray.length];
					for(int i = 0; i < goalsArray.length; i++) {
						legendLabels[i] = goalsArray[i].getName();
					}

					String legend_names = "";

					for(int i=0; i < goalsArray.length; i++) {
						if(i == (goalsArray.length-1)) {
							legend_names = legend_names + legendLabels[i];
						} else
							legend_names = legend_names + legendLabels[i] + "|";
					}

					WeightedGoal goal;
					for (int i = 0; i < goalsArray.length; i++) {
						goal = goalsArray[i];
						for (int j = 0; j < optionsArray.length; j++ ) {
							values[i][j] = ((double) matrix.getImpact(goal, optionsArray[j]).getValue());
							values[i][j] = values[i][j] * summedPreferences[i];
						}
					}

					int max_value = 0;
					for(int i = 0; i < optionsArray.length; i++) {
						int temp_value = 0;
						for(int j = 0; j < goalsArray.length; j++) {
							temp_value += values[j][i];
						}
						if(temp_value < 0) temp_value = -1 * temp_value;
						if(temp_value > max_value)
							max_value = temp_value;
					}
					if(max_value < 100) max_value = 100;

					String chartData = "";

					for(int i=0; i < goalsArray.length; i++) {
						if(i == (goalsArray.length-1)) {
							for(int j = 0; j < optionsArray.length; j++) {
								if(j == (optionsArray.length-1)) {
									chartData = chartData + values[i][j];
								} else
									chartData = chartData + values[i][j] + ",";
							}
						} else {
							for(int j = 0; j < optionsArray.length; j++) {
								if(j == (optionsArray.length-1)) {
									chartData = chartData + values[i][j];
								} else
									chartData = chartData + values[i][j] + ",";
							}
							chartData = chartData + "|";
						}
					}
					
					int[] chartValuesTotal = new int[optionsArray.length];
					
					for(int i = 0; i < optionsArray.length; i++) {
						for(int j = 0; j < goalsArray.length; j++) {
							chartValuesTotal[i] += values[j][i];
						}
					}
					
					String chartDataTotal = "";
					
					for(int i = 0; i < optionsArray.length; i++) {
						if(i == (optionsArray.length-1)) {
							chartDataTotal = chartDataTotal + chartValuesTotal[i];
						} else {
							chartDataTotal = chartDataTotal + chartValuesTotal[i] + ",";
						}
					}

					if((goalsArray.length + optionsArray.length) * 30 > 425) {
						int numDelete = (goalsArray.length + optionsArray.length) - 425/30;
						Window alert = new Window();
						alert.setWidth(310);
						alert.setHeight(400);
						alert.setPlain(false);
						alert.setHtml("<p><font size = 2> <b>WARNING</b>: Maximum graph size has been exceeded! </p> " +
								"<p>Too many options and goals have been selected. Please unselect " + numDelete + " goals/options. </p></font>" +
								"<img src = 'http://thumbs.dreamstime.com/thumb_376/1237296127yvKlEy.jpg' />");
						alert.setClosable(true);
						alert.setDraggable(false);
						alert.setResizable(false);
						alert.setCloseAction(Window.HIDE);
						alert.show();
						final ExtElement elm1 = Ext.get("accordion-values");
						elm1.unmask();
					} else {
						StringBuffer url1 = new StringBuffer();
						url1.append(PREFIX_URL);
						url1.append("cht=bhs");
						url1.append("&chs=700x").append(goalsArray.length*30 + optionsArray.length * 30); // size in pixels, height based on # of options
						url1.append("&chco=bf2a2a");
						url1.append("&chtt=Value%20of%20Options%20-%20All%20Goals"); // title
						url1.append("&chxt=x,y");
						url1.append("&chds=").append(-(max_value - max_value%100 + 100)).append(",").append((max_value - max_value%100 + 100));
						url1.append("&chd=t:");
						url1.append(chartDataTotal);
						url1.append("&chxl=1:|").append(value_names);
						url1.append("|0:|low%20value|zero|high%20value");
						HTML html1 = new HTML("<img src=\"" + url1.toString() + "\" title=\"Values chart\" />");		
						panel1.add(html1);
						
						StringBuffer url2 = new StringBuffer();
						url2.append(PREFIX_URL);
						url2.append("cht=bhs");
						url2.append("&chs=700x").append(goalsArray.length*30 + optionsArray.length * 30); // size in pixels, height based on # of options
						url2.append("&chco=bf2a2a");
						url2.append("&chtt=Value%20of%20Options%20-%20Each%20Goal"); // title
						url2.append("&chxt=x,y");
						url2.append("&chdl=");
						url2.append(legend_names);
						url2.append("&chco=4d89f9,48e766,f468ca,c6d9fd,e74863,5cb3ff,e4ec57,f52887,57ecbf&chdlp=b");
						url2.append("&chds=").append(-(max_value - max_value%100 + 100)).append(",").append((max_value - max_value%100 + 100));
						url2.append("&chd=t:");
						url2.append(chartData);
						url2.append("&chxl=1:|").append(value_names);
						url2.append("|0:|low%20value|zero|high%20value");
						HTML html2 = new HTML("<img src=\"" + url2.toString() + "\" title=\"Values chart\" />");		
						panel2.add(html2);
					}
				}
			});
		}
		if(user_index == length) return 0;

		return user_index;
	}

	public Option[] getOptions(TreeNode[] checkedOptionNodes, ImpactMatrix matrix) {
		//ArrayList<Option> options = new ArrayList<Option>();
		//For some reason the ArrayList did not work.

		Option[] tempOptions = matrix.getOptions();
		Option[] options = new Option[0];

		for(int i = 0; i < checkedOptionNodes.length; i++) {
			for(int j = 0; j < tempOptions.length; j++) {
				if(checkedOptionNodes[i].getText().trim().equals(tempOptions[j].getName().trim())) {
					Option[] optionsDelete = new Option[options.length+1];
					for(int k = 0; k < options.length; k++) {
						optionsDelete[k] = options[k];
					}
					optionsDelete[options.length] = tempOptions[j];
					options = optionsDelete;
				}
			}
		}
		return  options;
	}

	public WeightedGoal[] getGoals(TreeNode[] checkedGoalNodes, ImpactMatrix matrix) {
		WeightedGoal[] tempGoals = matrix.getGoals();
		WeightedGoal[] goals = new WeightedGoal[0];

		for(int i = 0; i < checkedGoalNodes.length; i++) {
			for(int j = 0; j < tempGoals.length; j++) {
				if(checkedGoalNodes[i].getText().trim().equals(tempGoals[j].getName().trim())) {
					WeightedGoal[] goalsDelete = new WeightedGoal[goals.length+1];
					for(int k = 0; k < goals.length; k++) {
						goalsDelete[k] = goals[k];
					}
					goalsDelete[goals.length] = tempGoals[j];
					goals = goalsDelete;
				}
			}
		}
		return  goals;
	}
}